//
//  NSObject+Custom.m
//  Application
//
//  Created by 薛小刚 on 2017/12/3.
//  Copyright © 2017年 薛小刚. All rights reserved.
//

#import "NSObject+Custom.h"


@implementation NSObject (Custom)

+ (CGFloat) getHeightWithString:(NSString *) textString font:(int) font labelWidth:(CGFloat) width {
    if (ISNULL(textString)) {
        return 0;
    }
    CGSize size = [textString boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:iphone6P ? font * 1.15 : font]} context:nil].size;
    return size.height;
}

+ (NSString *) getTime:(NSString *) timeStamp {
    if ([[timeStamp componentsSeparatedByString:@"."] firstObject].length > 10) {
        timeStamp = [NSString stringWithFormat:@"%f", [timeStamp doubleValue] / 1000];
    }
    NSDateFormatter * format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate * date = [NSDate dateWithTimeIntervalSince1970:[timeStamp doubleValue]];
    NSString * dateString = [format stringFromDate:date];
    return dateString;
}

+ (NSTimeInterval) getTimeStamp:(NSString *) time {
    NSArray * array = [time componentsSeparatedByString:@" "];
    if (array.count == 1) {
        time = [NSString stringWithFormat:@"%@ 00:00:00", time];
    } else {
        NSArray * array1 = [array.lastObject componentsSeparatedByString:@":"];
        if (array1.count == 1) {
            time = [NSString stringWithFormat:@"%@:00:00", time];
        } else if (array1.count == 2) {
            time = [NSString stringWithFormat:@"%@:00", time];
        } else {
            
        }
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //指定时间显示样式: HH表示24小时制 hh表示12小时制
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *lastTime = time;
    NSDate *lastDate = [formatter dateFromString:lastTime];
    //以 1970/01/01 GMT为基准，得到lastDate的时间戳
    long firstStamp = [lastDate timeIntervalSince1970];
    NSLog(@"firstStamp:%ld",firstStamp);
    return (firstStamp * 1000);
}

/**
 判断文字中是否包含有手机号码

 @param message 文字
 @return 返回手机号码
 */
+ (NSString *) getOtherWith:(NSString *) message {
    
    NSMutableString * string = [NSMutableString stringWithString:[message stringByReplacingOccurrencesOfString:@" " withString:@""]];
    HXLog(@"string is %@", string);
    if (string.length < 11) {
        return nil;
    }
    int index = 0;
    NSString * str = @"";
    [string stringByReplacingOccurrencesOfString:@"" withString:@" "];
    for (int i = 0; i < string.length; i++) {
        if ([string characterAtIndex:i] == '1') {
            if (index == 0) {
                index = i;
                for (int j = index; j < string.length; j++) {
                    if ([string characterAtIndex:j] >= '0' && [string characterAtIndex:j] <= '9') {
                        str = [NSString stringWithFormat:@"%@%c", str, [string characterAtIndex:j]];
                    } else {
                        if (str.length != 11) {
                            str = @"";
                        }
                    }
                }
                HXLog(@"str sdflkjsdflk %@", str);
                if (str.length != 11) {
                    str = @"";
                    index = 0;
                } else {
                    break;
                }
                
            }
        }
    }

    NSLog(@"str is  %@", str);
    return str;
}

+ (NSString *) getTimeTo:(NSString *) timeStamp {
    
    
    // 获取当前时时间戳 1466386762.345715 十位整数 6位小数
    NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
    // 创建歌曲时间戳(后台返回的时间 一般是13位数字)
    NSTimeInterval createTime = [timeStamp doubleValue]/1000;
    // 时间差
    NSTimeInterval time = currentTime - createTime;
    
    if (time < 60) {
        return @"刚刚";
    }
    NSInteger min = time / 60;
    if (min < 60) {
        return [NSString stringWithFormat:@"%ld分钟前",min];
    }
    // 秒转小时
    NSInteger hours = time / 3600;
    if (hours<24) {
        return [NSString stringWithFormat:@"%ld小时前",hours];
    }
    //秒转天数
    NSInteger days = time / 3600 / 24;
    if (days > 2) {
        return [[[[self class] getTime:timeStamp] componentsSeparatedByString:@" "] firstObject];
    } else {
        return @"昨天";
    }
    
    /*
    if (days < 30) {
        return [NSString stringWithFormat:@"%ld天前",days];
    }
    //秒转月
    NSInteger months = time /3600 /24 / 30;
    if (months < 12) {
        return [NSString stringWithFormat:@"%ld月前",months];
    }
    //秒转年
    NSInteger years = time /3600 /24 /30 / 12;
    return [NSString stringWithFormat:@"%ld年前",years];
    
    
    */
    
  
}

+ (NSString *)JSONStringFromObject:(id)object {
    NSData *data=[NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return jsonString;
}

+ (id)objectFromJSONString:(NSString*)jsonString {
    NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    id object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    return object;
}

+ (NSString *) getNumber:(NSString *) string {
    if (ISNULL(string)) {
        return @"0";
    } else {
        NSInteger count = [string integerValue];
        if (count > 1000) {
            return [NSString stringWithFormat:@"%.fk", count / 1000.];
        } else {
            return string;
        }
    }
}

@end
